package com.ruoyi.service.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.LocalDateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.sql.SqlUtil;
import com.ruoyi.service.account.entity.CompanyAccountDetailEntity;
import com.ruoyi.service.account.enums.company.ChangeTypeEnum;
import com.ruoyi.service.account.mapper.CompanyAccountDetailMapper;
import com.ruoyi.service.account.query.CompanyAccountQuery;
import com.ruoyi.service.account.service.CompanyAccountDetailService;
import com.ruoyi.service.project.enums.ProjectStatusEnum;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 公司账户变动明细 服务实现类
 * </p>
 *
 * @author Zane
 * @since 2025-10-19
 */
@Service
public class CompanyAccountDetailServiceImpl extends
    ServiceImpl<CompanyAccountDetailMapper, CompanyAccountDetailEntity>
    implements CompanyAccountDetailService {

  @Override
  public BigDecimal getPreDepositCurMonth() {
    final LocalDateTime begin = LocalDateUtils.beginCurrentMonth();
    final LocalDateTime end = LocalDateUtils.endCurrentMonth();
    return Optional.ofNullable(this.getBaseMapper()
            .getPreDepositInDays(begin, end, ChangeTypeEnum.ADD.getCode()))
        .orElse(BigDecimal.ZERO);
  }

  @Override
  public BigDecimal getPlatformFlows() {
    return Optional.ofNullable(this.getBaseMapper().getPlatformFlows(ChangeTypeEnum.ADD.getCode()))
        .orElse(BigDecimal.ZERO);
  }

  @Override
  public List<CompanyAccountDetailEntity> companyAccountList(CompanyAccountQuery query,
      Long companyId) {
    final LambdaQueryWrapper<CompanyAccountDetailEntity> queryWrapper = queryWrapper()
        .eq(CompanyAccountDetailEntity::getComId, companyId)
        .orderByDesc(CompanyAccountDetailEntity::getId);
    if (StringUtils.isNotBlank(query.getProjectName())) {
      //
//      LambdaQueryWrapper<ProjectEntity> subWrapper = new LambdaQueryWrapper<ProjectEntity>()
//          .select(ProjectEntity::getId)
//          .like(ProjectEntity::getProjectName, query.getProjectName())
//          .eq(ProjectEntity::getStatus, ProjectStatusEnum.SETTLED.getCode())
//          .eq(ProjectEntity::getComId, companyId);
      String subSql = SqlUtil.formatSql("select id from `t_project` where "
              + "`project_name` like '%{}%' and `status`='{}' and `com_id`='{}' ",
          query.getProjectName(), ProjectStatusEnum.SETTLED.getCode(), companyId
      );
      queryWrapper.inSql(CompanyAccountDetailEntity::getTraceKey, subSql);
    }
    return list(queryWrapper);
  }

  @Override
  public List<CompanyAccountDetailEntity> companyAccountListForPlatform(CompanyAccountQuery query) {
    final LambdaQueryWrapper<CompanyAccountDetailEntity> queryWrapper = queryWrapper()
        .orderByDesc(CompanyAccountDetailEntity::getId);
    if (StringUtils.isNotBlank(query.getProjectName())) {
      String subSql = SqlUtil.formatSql("select id from `t_project` where "
              + "`project_name` like '%{}%' and `status`='{}' ",
          query.getProjectName(), ProjectStatusEnum.SETTLED.getCode()
      );
      queryWrapper.inSql(CompanyAccountDetailEntity::getTraceKey, subSql);
    }
    return list(queryWrapper);
  }

  private LambdaQueryWrapper<CompanyAccountDetailEntity> queryWrapper() {
    return new LambdaQueryWrapper<>();
  }
}
